// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Collections.Generic;
using MatrixFramework.Core.Results;

namespace MatrixFramework.Keycloak.Abstractions.Models;

/// <summary>
/// 带搜索结果的分页结果包装类
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
public class PagedSearchResult<T> : ResultBase
{
    /// <summary>
    /// 分页数据
    /// </summary>
    public PagedResult<T> Data { get; private set; } = PagedResult<T>.Failure("未初始化");

    /// <summary>
    /// 私有构造函数
    /// </summary>
    private PagedSearchResult() { }

    /// <summary>
    /// 创建成功结果
    /// </summary>
    /// <param name="pagedResult">分页结果</param>
    /// <returns>搜索结果</returns>
    public static PagedSearchResult<T> Success(PagedResult<T> pagedResult)
    {
        return new PagedSearchResult<T>
        {
            IsSuccess = true,
            Data = pagedResult
        };
    }

    /// <summary>
    /// 创建失败结果
    /// </summary>
    /// <param name="error">错误信息</param>
    /// <returns>搜索结果</returns>
    public static PagedSearchResult<T> Failure(string error)
    {
        var result = new PagedSearchResult<T>();
        result.AddError(error);
        result.Data = PagedResult<T>.Failure(error);
        return result;
    }

    /// <summary>
    /// 创建失败结果
    /// </summary>
    /// <param name="errors">错误信息列表</param>
    /// <returns>搜索结果</returns>
    public static PagedSearchResult<T> Failure(List<string> errors)
    {
        var result = new PagedSearchResult<T>();
        if (errors != null && errors.Count > 0)
        {
            result.AddErrors(errors);
        }
        var errorList = errors ?? new List<string>();
        result.Data = PagedResult<T>.Failure(errorList);
        return result;
    }
}